package com.shop.dao;

import com.shop.model.Course;
import com.shop.model.Order;
import com.shop.model.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {
    public Boolean addOrder(String studentId, String courseId, double total_price,String status){
        int i = 0;
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try{
            connection = DBUtil.getConnection();
            String sql = "insert into orders(studentId,courseId,total_price,status) values(?, ?, ?, ?)";
            if(connection != null) {
                preparedStatement = connection.prepareStatement(sql);
            }
                preparedStatement.setString(1, studentId);
                preparedStatement.setString(2, courseId);
                preparedStatement.setDouble(3, total_price);
                preparedStatement.setString(4, status);

                i = preparedStatement.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection, preparedStatement);
        }
        if(i!=0){
            return true;
        }else{
            return false;
        }
    }

    public List<Order> queryOrder(String studentId){
        List<Order> orders = new ArrayList<>();
        Order order;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;


        try{
            connection = DBUtil.getConnection();
            String sql = "select * from orders where studentId = ?";
            if(connection != null) {
                preparedStatement = connection.prepareStatement(sql);
            }
            preparedStatement.setString(1, studentId);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                order = new Order(
                        resultSet.getInt("orderId"),
                        resultSet.getString("status"),
                        resultSet.getString("studentId"),
                        resultSet.getString("courseId"),
                        resultSet.getDouble("total_price"),
                        resultSet.getObject("order_date", LocalDateTime.class)
                );
                orders.add(order);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection, preparedStatement, resultSet);
        }
        return orders;
    }

    public Boolean include_course(String courseId,String studentId){
        boolean include = true;

        try(Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement =
                    connection.prepareStatement("SELECT 1 FROM orders WHERE courseId = ? and studentId = ?")){
            preparedStatement.setString(1, courseId);
            preparedStatement.setString(2, studentId);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                include = !resultSet.next();
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return include;
    }

    public List<Course> getCourses(String studentId){
        List<Course> courses = new ArrayList<>();
        Course course;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try{
            connection = DBUtil.getConnection();
            String sql = "select * from course where courseId in (select courseId from orders where studentId = ? and status = '已付款')";
            if(connection != null) {
                preparedStatement = connection.prepareStatement(sql);
            }
            preparedStatement.setString(1, studentId);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                course = new Course(
                        resultSet.getString("courseName"),
                        resultSet.getString("courseId"),
                        resultSet.getString("Teacher"),
                        resultSet.getDouble("price"),
                        resultSet.getDate("startDate"),
                        resultSet.getDate("endDate"),
                        resultSet.getString("type"),
                        resultSet.getInt("capacity"),
                        resultSet.getInt("time")
                );
                courses.add(course);
            }
        }catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection, preparedStatement, resultSet);
        }
        return courses;
    }

}
